package com.example.hw2.controller;

import com.example.hw2.dao.LessonRepository;
import com.example.hw2.dao.StudentRepository;
import com.example.hw2.entity.Lesson;
import com.example.hw2.entity.Student;
import com.example.hw2.service.InterLesson;
import com.example.hw2.service.InterStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private LessonRepository lessonRepository;
    InterStudent interStudent;
    InterLesson interLesson;
    private Student s;
    private List<Lesson> checkLesson;

    @GetMapping("/JumpStudent")
    public String addStudent(Model model){
        s=new Student();
        model.addAttribute("Student",s);
        return "PersonInformation";
    }

    @GetMapping("/Lesson")
    public String jumpLesson(@Validated @ModelAttribute("Student") Student student,
                             @RequestParam("name") String name,@RequestParam("grade") String grade,
                             @RequestParam("major") String major,@RequestParam("college") String college,
                             @RequestParam("id") String id,
                             @RequestParam("selectAcademy") String selectAcademy,
                             @RequestParam("selectMajor") String selectMajor,
                             Model model){
        s = new Student();
        s = student;
        s.setSName(name);
        s.setSGrade(grade);
        s.setSMajor(major);
        s.setSCollege(college);
        s.setSId(id);

        //添加课程
        List<Lesson> lessons= Arrays.asList(
                /*new Lesson("高等数学"),
                new Lesson("Java基础"),
                new Lesson("阿水变成阿淼——成神之路"),
                new Lesson("论演员的自我修养"),
                new Lesson("计算机组成原理"),
                new Lesson("JavaWeb")*/
        );
        lessonRepository.findAll(lessons);
        model.addAttribute("Student",s);
        interStudent.add(s);
        return "LessonInformation";
    }


    @PostMapping("/Lesson")
    public String showTotalLesson(Model model, HttpServletRequest request){
        checkLesson = new ArrayList<>();
        String[] lessonsNums = request.getParameterValues("checkLessons");
        for (int i=0;i<lessonsNums.length;i++) {
            checkLesson.add(new Lesson(lessonsNums[i]));
        }
        //s.setLessonList(checkLesson);

        model.addAttribute("Student",s);
        model.addAttribute("lessons",checkLesson);
        interLesson.add(checkLesson);
        return "ShowLesson";
    }
}
